package spotGUI.mainFrameGUI;

import java.awt.CardLayout;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GraphicsEnvironment;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JToggleButton;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import managers.messengerManager.MessengerManager;
import spotGUI.sideBar.MainSideBar;

public class MainFrame extends JFrame {


	private StatusBar statusBar;
	private MainSideBar mainSideBar;
	private JPanel panel;
	private PersonalStateBar personalStateBar;
	private JToggleButton tglbtnSwitch;
	private CardLayout cardLayout;

	private MessengerMainPanel messengerPanel;
	private SocialNetworkMainPanel socialNetworkPanel;
	private JLabel image;
	private JLabel lblTitle;

	private final ImageIcon chatMode = new ImageIcon(MainFrame.class.getResource("/images/GUIBackgrounds/chatMode.png"));
	private final ImageIcon socialMode = new ImageIcon(MainFrame.class.getResource("/images/GUIBackgrounds/socialMode.png"));
	private JScrollPane scrollPane;
	
	private MessengerManager manager = MessengerManager.getSharedInstance();
	private WindowListener windowListener = new WindowAdapter() {
		
		@Override
		public void windowClosing(WindowEvent e) {
			manager.getFeedbackManager().showTrayIcon(true);
		}
		
		@Override
		public void windowActivated(WindowEvent e) {
			manager.getFeedbackManager().showTrayIcon(false);
		}
	};


	public MainFrame() {
		
		setTitle("Spot Messenger");
		
		GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
		setMaximizedBounds(env.getMaximumWindowBounds());
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		
		setIconImage(Toolkit.getDefaultToolkit().getImage(MainFrame.class.getResource("/images/Icons/64x64/users_comments.png")));
		setMinimumSize(new Dimension(800, 600));
		setSize(new Dimension(800, 765));
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setFocusTraversalPolicy(null);
		
		addWindowListener(windowListener);

		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{115, -208, 228, 0};
		gridBagLayout.rowHeights = new int[]{64, 31, 195, 37, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 1.0, 0.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);

		lblTitle = new JLabel("");
		lblTitle.setIcon(chatMode);
		GridBagConstraints gbc_lblTitle = new GridBagConstraints();
		gbc_lblTitle.anchor = GridBagConstraints.WEST;
		gbc_lblTitle.insets = new Insets(0, 0, 5, 5);
		gbc_lblTitle.gridx = 1;
		gbc_lblTitle.gridy = 0;
		getContentPane().add(lblTitle, gbc_lblTitle);

		scrollPane = new JScrollPane();
		scrollPane.setBorder(null);
		scrollPane.setOpaque(false);
		scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridheight = 3;
		gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
		gbc_scrollPane.gridx = 2;
		gbc_scrollPane.gridy = 0;
		getContentPane().add(scrollPane, gbc_scrollPane);

		mainSideBar = new MainSideBar();
		scrollPane.setViewportView(mainSideBar);



		personalStateBar = new PersonalStateBar();
		GridBagConstraints gbc_personalStateBar = new GridBagConstraints();
		gbc_personalStateBar.anchor = GridBagConstraints.NORTH;
		gbc_personalStateBar.insets = new Insets(0, 0, 5, 5);
		gbc_personalStateBar.fill = GridBagConstraints.HORIZONTAL;
		gbc_personalStateBar.gridx = 1;
		gbc_personalStateBar.gridy = 1;
		getContentPane().add(personalStateBar, gbc_personalStateBar);

		panel = new JPanel();
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridwidth = 2;
		gbc_panel.insets = new Insets(0, 0, 5, 5);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 2;
		getContentPane().add(panel, gbc_panel);

		cardLayout = new CardLayout(0, 0);
		panel.setLayout(cardLayout);

		messengerPanel = new MessengerMainPanel();
		panel.add(messengerPanel, "Messenger");
		socialNetworkPanel = new SocialNetworkMainPanel();
		panel.add(socialNetworkPanel, "Social");

		statusBar = new StatusBar();
		GridBagConstraints gbc_sb = new GridBagConstraints();
		gbc_sb.fill = GridBagConstraints.HORIZONTAL;
		gbc_sb.anchor = GridBagConstraints.SOUTH;
		gbc_sb.gridwidth = 2;
		gbc_sb.insets = new Insets(0, 0, 0, 5);
		gbc_sb.gridy = 3;
		gbc_sb.gridx = 0;
		getContentPane().add(statusBar, gbc_sb);

		image = new JLabel("");
		image.setIcon(new ImageIcon(MainFrame.class.getResource("/images/GUIBackgrounds/logo.png")));
		GridBagConstraints gbc_image = new GridBagConstraints();
		gbc_image.gridx = 2;
		gbc_image.gridy = 3;
		getContentPane().add(image, gbc_image);
		
		tglbtnSwitch = new JToggleButton("");
		tglbtnSwitch.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR));
		tglbtnSwitch.setFocusPainted(false);
		tglbtnSwitch.setToolTipText(ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("MainFrame.1"));
		tglbtnSwitch.setVerticalTextPosition(SwingConstants.BOTTOM);
		tglbtnSwitch.setContentAreaFilled(false);
		tglbtnSwitch.setBorder(new EmptyBorder(10, 10, 0, 0));
		tglbtnSwitch.setBorderPainted(false);
		tglbtnSwitch.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent arg0) {
				if (arg0.getStateChange() == ItemEvent.DESELECTED) {
					cardLayout.first(panel);
					tglbtnSwitch.setToolTipText(ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("MainFrame.1"));
					lblTitle.setIcon(chatMode);
				} else {
					cardLayout.last(panel);
					tglbtnSwitch.setToolTipText(ResourceBundle.getBundle("spotGUI.mainFrameGUI.messages").getString("MainFrame.2"));
					lblTitle.setIcon(socialMode);
				}
			}
		});
		tglbtnSwitch.setSelectedIcon(new ImageIcon(MainFrame.class.getResource("/images/Icons/128x128px/rss.png")));
		tglbtnSwitch.setIcon(new ImageIcon(MainFrame.class.getResource("/images/Icons/128x128px/comment.png")));
		GridBagConstraints gbc_tglbtnSwitch = new GridBagConstraints();
		gbc_tglbtnSwitch.gridheight = 2;
		gbc_tglbtnSwitch.anchor = GridBagConstraints.WEST;
		gbc_tglbtnSwitch.fill = GridBagConstraints.VERTICAL;
		gbc_tglbtnSwitch.insets = new Insets(0, 0, 5, 5);
		gbc_tglbtnSwitch.gridx = 0;
		gbc_tglbtnSwitch.gridy = 0;
		getContentPane().add(tglbtnSwitch, gbc_tglbtnSwitch);

	}



	public MessengerMainPanel getMessengerPanel() {
		return messengerPanel;
	}

	public SocialNetworkMainPanel getSocialNetworkPanel() {
		return socialNetworkPanel;
	}

	public PersonalStateBar getPersonalStateBar () {
		return personalStateBar;
	}

	public StatusBar getStatusBar () {
		return statusBar;
	}

}
